package com.pengtu.gsj.controller.jsbz;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pengtu.gsj.controller.base.DefaultBaseController;
import com.pengtu.gsj.dao.base.Page;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.entity.jsbz.RoadMaterial;
import com.pengtu.gsj.interceptor.Token;
import com.pengtu.gsj.service.jsbz.RoadMaterialService;
import com.pengtu.gsj.utils.MathsUtils;

/**
 * 路面材料循环利用
 * 2017年6月14日 上午11:34:13
 * @author heyong
 * @version 1.0.0
 *
 */

@Controller
@RequestMapping("roadMaterial")
public class RoadMaterialController extends DefaultBaseController {

	@Autowired
	private RoadMaterialService roadMaterialService;
	
	/**
	 * 加载
	 * @param id
	 * @return
	 */
	@ModelAttribute
	public RoadMaterial load(String id){
		RoadMaterial roadMaterial;
		if(StringUtils.isNotEmpty(id)){
			roadMaterial = roadMaterialService.getRoadMaterialById(id);
		}else{
			roadMaterial = new RoadMaterial();
		}
		return roadMaterial;
	}
	
	
	/**
	 * 页面跳转
	 * @return
	 */
	@RequestMapping("listInfo")
	@Token(save = true)
	public String showLisfInfo() {
		return "jsbz/roadMaterial_list";
	}
	
	
	@RequestMapping(value="list", produces = "application/json; charset=utf-8")
	@ResponseBody
	public Page<RoadMaterial> getRoadMaterialList(Page<RoadMaterial> page, HttpServletRequest request){
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(request);
		if (!page.isOrderBySetted()) {
			page.setOrderBy("testYear");
			page.setOrder(Page.ASC);
		}
		page = roadMaterialService.findRoadMaterial(page,queryFilter);
		return page;
	}
	
	/**
	 * 保存
	 * @param roadMaterial
	 * @return
	 */
	@RequestMapping(value = "save", method=RequestMethod.POST)
	@ResponseBody
	@Token(remove = true)
	public boolean saveRoadMaterial(@ModelAttribute RoadMaterial roadMat){
		roadMat.setTotal(roadMat.getAsphalMaterial() + roadMat.getCementMaterial());
		roadMat.setRecyclingRate(MathsUtils.divDouble((roadMat.getAsphalMaterial() + roadMat.getCementMaterial()) * 100, (roadMat.getAsphaltLayer() + roadMat.getCementLayer()), 2));
		roadMaterialService.saveRoadMaterial(roadMat);
		return true;
	}
	
	
	/**
	 * 加载数据
	 * @param infoManagement
	 * @return
	 */
	@RequestMapping(value="show")
	@ResponseBody                                                     
	public RoadMaterial showInfoManagement (@ModelAttribute RoadMaterial roadMaterial){
		return roadMaterial;
	}
	
	/**
	 * 删除
	 * @param infoManagement
	 * @return
	 */
	@RequestMapping(value="delete")
	@ResponseBody
	public boolean delateInfoManagement(@ModelAttribute RoadMaterial roadMaterial){
		roadMaterialService.delateRoadMaterial(roadMaterial);
		return true;
	}
	
}
